<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<script type="text/javascript">
	function ciPropertyTree_OnClick(e, treeId, treeNode) {
		var treeObj = ciPropertyTree.zTree;
		var nodes = treeObj.getCheckedNodes();
		for ( var i = 0, l = nodes.length; i < l; i++) {
			treeObj.checkNode(nodes[i], false, false);
		}

		nodes = treeObj.getSelectedNodes();
		for ( var i = 0, l = nodes.length; i < l; i++) {

			treeObj.cancelSelectedNode(nodes[i]);
		}

		treeObj.selectNode(treeNode);
		treeObj.checkNode(treeNode, !treeNode.checked, null, true);
	}
</script>
<div id="importContainer" class="wizard box tabbedBox clearfix ">
	<div class=" header">
		<ul class="steps">
			<li><a class="current" href="#wiz_importBaseInfo">1、基础信息</a></li>
			<li><a href="#wiz_importExecute">2、导入信息设定</a></li>
		</ul>
	</div>
	<div style="padding:0" class="content tabbed">
		<div id="wiz_importBaseInfo">
			<%-- <div class="box" style="display:block !important">
				<div class="header">
					<h2 style="text-align:left">1.1 选择目标对象</h2>
				</div>
				<table class="form-table">

					<c:forEach items="${entities}" var="entities">
						<tr>
							<td class="td1">${entities.key}：</td>
							<td class="td2"><c:forEach items="${entities.value}"
									var="entity" varStatus="idx">
									<input type="radio" value="${entity.name }" id="e_${entity.id}"
										name="entity_name" class="radio_entity_name">
									<label for="e_${entity.id}"> ${entity.name }</label>
									<c:if test="${'资产' eq entity.name }">
										<div id="ciPropertyTreePanel" class="treePanel2"
											style="width:400px;height:300px;display:none;overflow: auto;">

											<isperp:tree id="ciPropertyTree" modal="readonly">
												<isperp:async enable="true" url=""></isperp:async>
												<isperp:check enable="true" chkStyle="radio" radioType="all"></isperp:check>
												<isperp:callback onClick="ciPropertyTree_OnClick"
													onCheck="function(){return false;}"></isperp:callback>
												<isperp:view nameIsHTML="true"></isperp:view>
											</isperp:tree>

										</div>
									</c:if>
								</c:forEach>
							</td>
						</tr>
					</c:forEach>
					<tr>

						<td colspan="2" class="td2" style="padding:5px">导入模板下载：<span
							class="icon icon-file"></span>&nbsp;<a class="nui-txt-link"
							href="javascript:downloadTemplateFile();">点击下载模板 </a>
						</td>
					</tr>
				</table>
			</div> --%>
			<div class="box">
				<div class="header">
					<h2 style="text-align:left">1.1 上传数据文件</h2>
				</div>
				<table class="form-table">
				   <tr>
						<td class="td1">说明：</td>
						<td class="td2">
						  <span>
						     1.可以直接从【资产管理】-【资产档案】点击【 导出到Excel】按钮获取导入模板
						     <br>
						     2.导出的模板不包括扩展属性列，请在基本属性列最后的空白列<span style="color:red">自行添加扩展属性列,且导入的资产分类需一样</span>
						  </span>
						  <br><br>
						  <img alt="" src="img/ci/ciimport1.png" >
						</td>
					</tr>
					<tr>
						<td class="td1">类型：</td>
						<td class="td2"><select>
								<option selected="selected">Excel文件</option>
						</select></td>
					</tr>
					
					<tr>
						<td class="td1">请选择文件：</td>
						<td class="td2"><input type="hidden" id="excelFilePath">
							<form id="importFileForm" action="fileUpload" method="post"
								enctype="multipart/form-data">
								<input id="excelFile" name="excelFile" type="file"
									style="width:30%">（仅支持2003、2007、2010Excel格式）
							</form>
						</td>
					</tr>
				</table>
			</div>

		</div>

		<div id="wiz_importExecute">
			<div class="box">
				<div class="header">
					<h2 style="text-align:left">2.1 数据项匹配</h2>
				</div>

				<table class="form-table">
					<tr>
						<td class="td1"></td>
						<td class="td2"><div id="importSheetSelectTr"></div>
						</td>
				</table>
				<table class="form-table" id="importSheetTable">
				</table>
			</div>
			<div class="box">
				<div class="header">
					<h2 style="text-align:left">2.2 数据导入方式</h2>
				</div>
				<div style="padding:0 10px">

					请选择导入策略：<br> <input id="importType1" name="importType"
						value="1" type="radio" checked="checked"><label
						for="importType1">覆盖：保留旧数据，若存在则更新，若不存在则插入</label><br /> <input
						id="importType0" name="importType" value="0" type="radio"><label
						for="importType0">插入：删除旧数据，全部重新插入（慎用）</label> <br />

				</div>
				<div id="importResultMessage"
					style="padding:10px;color:red;font-size: 14px;width:95%;"></div>
			</div>
		</div>
	</div>
	<isperp:wizButtonList buttonListId="wizCreateSetpBtns"></isperp:wizButtonList>

</div>
<script type="text/javascript">
	$(document).ready(function() {

		//	isperp.initElement("#importContainer");
		var wizCreate = isperp.wizard({
			wizardId : "importContainer"
		});

		$(".radio_entity_name").change(function() {
			if ($(this).val() == "资产") {

				var treeObj = $.fn.zTree.getZTreeObj("ciPropertyTree");
				treeObj.setting.async.url = "configuration/category/getTree";

				ciPropertyTree.reload();
				$("#ciPropertyTreePanel").show();

			} else {
				$("#ciPropertyTreePanel").hide();
			}

		});

	});

	var excelColumn = null;

	var entityField = null;

	function checkSlectedNode(entityName) {

		/* if (!entityName || entityField == "") {
			isperp.alert("请选择目标对象！");
			return false;
		}
		if (entityName == "资产") {
			var nodes = ciPropertyTree.zTree.getCheckedNodes(true);
			if (nodes.length == 0 || nodes[0].id == "") {
				isperp.alert("请选择资产分类！");
				return false;
			}

		} */
		return true;
	}

	var importContainer_Event1 = function(index, obj) {

		var entityName = "资产";

		if (checkSlectedNode(entityName) == false) {
			return false;
		}

		var excelFile = $("#excelFilePath").val();

		if (!excelFile || excelFile == "") {

			if ($("#excelFile").val() != "") {
				//增加朦胧层阻止用户操作，等待上传完成后清除
				isperp.showLoading();
				isperp.submitForm({
					formId : "importFileForm",
					success : function(data) {
						var result = data;
						$("#excelFilePath").val(result.path);
						listEntityFieldAndExcelColumn(entityName, result.path);
						isperp.hideLoading();
					}

				});
			} else {
				isperp.alert("导入文件未上传，请上传文件！");
				return false;
			}

		} else {
			listEntityFieldAndExcelColumn(entityName, excelFile);
		}

	};

	var importContainer_Event2 = function(index, obj) {

		var map = "[";
		$("#importSheetTable .td1").each(function(index, item) {

			var excelColumn = $(this).next().children("select").val();

			var entityField = $(this).html();

			if (index != 0)
				map += ",";
			map += "{'excelColumn':'" + excelColumn + "','entityField':'" + entityField + "'}";

		});
		map += "]";

		var entityName = $("input[name='entity_name']:checked").val();
		var sheetName = $("#importSheetSelect").val();
		//增加朦胧层，等待导入完成后清除
		var impResult = false;
		isperp.showLoading();

		/* var nodes = ciPropertyTree.zTree.getCheckedNodes(true);
		var categoryId = "";
		var categoryName = "";
		if (nodes && nodes.length > 0) {
			categoryId = nodes[0].id;
			categoryName = nodes[0].treenodeNamePath;
		} */
		var importType = $("input[name='importType']:checked").val();
		$.ajax({
			async : false,
			url : "db/import/ciImp",
			data : {
				"filePath" : $("#excelFilePath").val(),
				"map" : map,
				"importType" : importType,
				"entityName" : '资产',
				"sheetName" : sheetName,
				"categoryId" : '',
				"categoryName" : '',
				"ty" : '${ty}',
				"relationId" : '${relationId}'
			},
			dataType : "json",
			success : function(data) {

				isperp.hideLoading();
				var $el = $("#importResultMessage");
				$el.empty();
				if (data.success) {
					impResult = true;
					isperp.alert("导入成功 ！");
				} else {
					$el.html("<div class='alert-error' style='float:left;height:30px'></div><div  style='float:left;margin-left:5px'>" + data.msg + "</div>");
				}
			},
			error : function(data) {
				isperp.alert(data.responseText);
				isperp.hideLoading();
			}

		});

		return false;//停留在步骤2
	};

	function importSheetSelect_OnChange() {
		var sheet = $("#importSheetSelect").val();

		var columns = excelColumn[sheet];
		var sheetSelect = "<select><option></option>";
		$(columns).each(function(index, item) {
			sheetSelect += "<option value='"+item.excelColumn+"'>" + item.excelColumn + "</option>";

		});
		sheetSelect += "</select>";

		$("#importSheetTable .td2").each(function(index, item) {
			$(this).empty();
			$(this).append(sheetSelect);

			var $prev = $(this).prev();

			var $select = $(this).children().get(0);
			$($select).val($prev.text());

		});

		//设置默认值

		isperp.initElement("#importSheetTable");
	}

	function listEntityFieldAndExcelColumn(entityName, excelFile) {

		$.getJSON("db/entity/listEntityField?name=" + entityName, function(data) {
			entityField = data;
			var rStr = "";
			$(entityField).each(function(index, item) {
				if (item.useForImport)
					rStr += "<tr><td class='td1'>" + item.name + "</td><td class='td2'></td></tr>";
			});
			$("#importSheetTable").empty();

			$("#importSheetTable").append(rStr);
			getExcelColumn(entityName, excelFile);

		});
	}

	function getExcelColumn(entityName, excelFile) {

		$.getJSON("db/import/getExcelFileInfo", {
			"entityName" : entityName,
			"filePath" : excelFile
		}, function(data) {

			var sheetSelect = "<select id='importSheetSelect' " + "onchange='importSheetSelect_OnChange();'>";
			for ( var i in data) {
				sheetSelect += "<option value='"+i+"'>" + i + "</option>";
			}
			sheetSelect += "</select>";
			$("#importSheetSelectTr").empty();

			$("#importSheetSelectTr").append(sheetSelect);
			excelColumn = data;

			isperp.initElement("#importSheetSelectTr");

			importSheetSelect_OnChange();
		});

	}

	function downloadTemplateFile() {

		var entityName = $("input[name='entity_name']:checked").val();

		if (checkSlectedNode(entityName) == false) {
			return false;
		}

		var entityCategoryId = "";
		var entityCategoryName = "";
		if (entityName == "资产") {
			var nodes = ciPropertyTree.zTree.getCheckedNodes(true);
			entityCategoryId = nodes[0].id;
			entityCategoryName = nodes[0].name;
		}

		$.get("db/import/getImportExcelModel", {
			"entityName" : entityName,
			"entityCategoryId" : entityCategoryId,
			"entityCategoryName" : entityCategoryName

		}, function(data) {
			window.open(data, '_self');
		});

	}
</script>

